package cloud.tianai.mate.captcha.validator;

import cloud.tianai.captcha.common.AnyMap;
import cloud.tianai.captcha.common.util.CaptchaTypeClassifier;
import cloud.tianai.captcha.common.util.ObjectUtils;
import cloud.tianai.captcha.validator.common.constant.TrackTypeConstant;
import cloud.tianai.captcha.validator.common.model.dto.ImageCaptchaTrack;
import cloud.tianai.captcha.validator.impl.SimpleImageCaptchaValidator;
import cloud.tianai.neuron.common.NumPy;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static cloud.tianai.mate.captcha.common.constant.MateCaptchaTypeConstant.*;

/**
 * @Author: 天爱有情
 * @date 2023/11/9 14:47
 * @Description 基于开源SimpleImageCaptchaValidator进行增强
 */
public class EnhanceSimpleImageCaptchaValidator extends SimpleImageCaptchaValidator {

    public EnhanceSimpleImageCaptchaValidator() {
        super();
        CaptchaTypeClassifier.addClickCaptchaType(HONEYCOMB_IMAGE_CLICK);
        CaptchaTypeClassifier.addClickCaptchaType(IMAGE_CLICK);
        CaptchaTypeClassifier.addJigsawCaptchaType(JIGSAW);
        CaptchaTypeClassifier.addSliderCaptchaType(ROTATE_DEGREE);
        CaptchaTypeClassifier.addClickCaptchaType(WORD_ORDER_IMAGE_CLICK);
        CaptchaTypeClassifier.addSliderCaptchaType(SCRAPE);
        CaptchaTypeClassifier.addSliderCaptchaType(CURVE);
        CaptchaTypeClassifier.addSliderCaptchaType(SLIDER2);
    }

    @Override
    public boolean doValidClickCaptcha(ImageCaptchaTrack imageCaptchaTrack, AnyMap imageCaptchaValidData, Float tolerant, String type) {
        String validStr = imageCaptchaValidData.getString(PERCENTAGE_KEY, null);
        if (ObjectUtils.isEmpty(validStr)) {
            return false;
        }
        String[] splitArr = validStr.split(";");
        List<ImageCaptchaTrack.Track> trackList = imageCaptchaTrack.getTrackList();
        if (trackList.size() < splitArr.length) {
            return false;
        }
        // 取出点击事件的轨迹数据
        List<ImageCaptchaTrack.Track> clickTrackList = trackList
                .stream()
                .filter(t -> TrackTypeConstant.CLICK.equalsIgnoreCase(t.getType()))
                .collect(Collectors.toList());
        if (clickTrackList.size() != splitArr.length) {
            return false;
        }
        StringBuilder sb = new StringBuilder();
        List<Double> percentages = new ArrayList<>();
        for (int i = 0; i < splitArr.length; i++) {
            ImageCaptchaTrack.Track track = clickTrackList.get(i);
            String posStr = splitArr[i];
            String[] posArr = posStr.split(",");
            float xPercentage = Float.parseFloat(posArr[0]);
            float yPercentage = Float.parseFloat(posArr[1]);

            float calcXPercentage = calcPercentage(track.getX(), imageCaptchaTrack.getBgImageWidth());
            float calcYPercentage = calcPercentage(track.getY(), imageCaptchaTrack.getBgImageHeight());
            if (!checkPercentage(calcXPercentage, xPercentage, tolerant)
                    || !checkPercentage(calcYPercentage, yPercentage, tolerant)) {
                return false;
            }
            if (i > 0) {
                sb.append("|");
            }
            sb.append(calcXPercentage).append(",").append(calcYPercentage);
            percentages.add((double) ((calcXPercentage - xPercentage) + (calcYPercentage - yPercentage)));
        }
        // 存储一下当前计算出来的值
        imageCaptchaValidData.put(USER_CURRENT_PERCENTAGE, sb.toString());
        imageCaptchaValidData.put(USER_CURRENT_PERCENTAGE_STD, String.valueOf(NumPy.std(percentages)));
        return true;
    }
}
